/**
 * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.opsli.modulars.ykfire.ykcheck.web;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.map.HashedMap;
import org.opsli.api.wrapper.ykfire.ykcompany.YkcompanyModel;
import org.opsli.api.wrapper.ykfire.ykuser.YkuserModel;
import org.opsli.common.annotation.RequiresPermissionsCus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.opsli.api.base.result.ResultVo;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.common.annotation.EnableLog;
import org.opsli.core.base.controller.BaseRestController;
import org.opsli.core.persistence.Page;
import org.opsli.core.persistence.querybuilder.GenQueryBuilder;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.opsli.core.persistence.querybuilder.WebQueryBuilder;
import org.opsli.modulars.ykfire.ykarea.entity.Ykarea;
import org.opsli.modulars.ykfire.ykarea.service.IYkareaService;
import org.opsli.modulars.ykfire.ykcheck.entity.Ykisorno;
import org.opsli.modulars.ykfire.ykcompany.entity.Ykcompany;
import org.opsli.modulars.ykfire.ykcompany.service.IYkcompanyService;
import org.opsli.modulars.ykfire.ykrecord.entity.Ykrecord;
import org.opsli.modulars.ykfire.ykrecord.service.IYkrecordService;
import org.opsli.modulars.ykfire.ykuser.entity.Ykuser;
import org.opsli.modulars.ykfire.ykuser.service.IYkuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import org.opsli.modulars.ykfire.ykcheck.entity.Ykcheck;
import org.opsli.api.wrapper.ykfire.ykcheck.YkcheckModel;
import org.opsli.modulars.ykfire.ykcheck.service.IYkcheckService;
import org.opsli.api.web.ykfire.ykcheck.YkcheckRestApi;

/**
 * ykcheck Controller
 *
 * @author cy
 * @date 2021-06-04 12:08:26
 */
@Api(tags = YkcheckRestApi.TITLE)
@Slf4j
@ApiRestController("/ykfire/ykcheck")
public class YkcheckRestController extends BaseRestController<Ykcheck, YkcheckModel, IYkcheckService>
    implements YkcheckRestApi {

    @Autowired
    private IYkrecordService iYkrecordService;

    @Autowired
    private IYkuserService iYkuserService;

    @Autowired
    private IYkareaService iYkareaService;

    @Autowired
    private IYkcompanyService iYkcompanyService;
    /**
     * 检查信息表 提交
     * @param formid 表id
     * @return ResultVo
     */
    @ApiOperation(value = "提交检查信息表", notes = "提交检查信息表")
//    @RequiresPermissions("ykfire_ykcheck_sendfirst")
    @EnableLog
    @PostMapping("/sendfirst")
    public ResultVo<?> sendfirst(String formid) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        if (IService.get(formid).getStatus().equals("0")) {
            int count=0;

            // 修改状态为 1 已提交
            YkcheckModel ykcheck = IService.get(formid);
            ykcheck.setStatus("1");

            Ykisorno ykisorno = new Ykisorno();
            //转换 ykcheck 至 ykisorno
            BeanUtils.copyProperties(ykisorno, ykcheck);

            Field[] field = ykisorno.getClass().getDeclaredFields();
            for (int j = 0; j < 37; j++) {
                //获取字段名
                String name = field[j].getName();
                name = name.substring(0, 1).toUpperCase() + name.substring(1);
                Method m = ykisorno.getClass().getMethod("get" + name);
                // 调用getter方法获取属性值
                String value = (String) m.invoke(ykisorno);

                if (value.equals("1")) {
                    //如果存在 1 有问题，更改correct为1 需整改
                    System.out.println("是否需要整改1=============="+ykcheck.getCorrect());

                    //生成整改表
                    Ykrecord ykrecord = new Ykrecord();
                    ykrecord.setFormId(ykcheck.getId());
                    ykrecord.setFormType(ykcheck.getType());
                    ykrecord.setInspectorId(ykcheck.getPersonId());
                    ykrecord.setInspectorName(ykcheck.getPersonName());
                    ykrecord.setAdviserId(ykcheck.getAdviserId());
                    ykrecord.setAdviserName(ykcheck.getAdviserName());
                    ykrecord.setCompanyId(ykcheck.getCompanyId());
                    ykrecord.setCompanyName(ykcheck.getCompanyName());
                    ykrecord.setAreaId(ykcheck.getAreaId());
                    ykrecord.setProblem(name);

                    count++;
                    iYkrecordService.save(ykrecord);
                }
            }

            if(count>0){
                ykcheck.setCorrect("1");
            }else ykcheck.setCorrect("0");
            IService.update(ykcheck);
            return ResultVo.success("提交检查表成功");
        }
        else return ResultVo.error("请勿重复提交");
    }



    /**
     * 审核员审核
     * @param
     * @return ResultVo
     */
    @ApiOperation(value = "审核员审核", notes = "审核员审核")
//    @RequiresPermissions("ykfire_ykcheck_select")
    @PostMapping("/aduitor")
    public ResultVo<?> aduitor(String formid) {
        YkcheckModel ykcheck=IService.get(formid);
        ykcheck.setStatus("2");
        IService.update(ykcheck);
        return ResultVo.success("检查表审核中");
    }

    /**
     * 参谋长审核
     * @param
     * @return ResultVo
     */
    @ApiOperation(value = "参谋长审核", notes = "参谋长审核")
//    @RequiresPermissions("ykfire_ykcheck_select")
    @PostMapping("/adviser")
    public ResultVo<?> adviser(String formid) {
        YkcheckModel ykcheck=IService.get(formid);
        ykcheck.setStatus("3");

        QueryBuilder<Ykrecord> queryBuilder = new GenQueryBuilder<>();
        QueryWrapper<Ykrecord> queryWrapper = queryBuilder.build();
        queryWrapper.eq("form_id",formid);
        List<Ykrecord>list=iYkrecordService.findList(queryWrapper);
        for(int i=0;i<list.size(); i++){
            iYkrecordService.delete(list.get(i).getId());
        }

        IService.update(ykcheck);
        return ResultVo.success("检查表已完结");
    }



    /**
    * 检查信息表 查一条
    * @param model 模型
    * @return ResultVo
    */
    @ApiOperation(value = "获得单条检查信息表", notes = "获得单条检查信息表 - ID")
//    @RequiresPermissions("ykfire_ykcheck_select")
    @Override
    public ResultVo<YkcheckModel> get(YkcheckModel model) {
        // 如果系统内部调用 则直接查数据库
        if(model != null && model.getIzApi() != null && model.getIzApi()){
            model = IService.get(model);
        }
        return ResultVo.success(model);
    }

    /**
    * 检查信息表（不同角色） 查询分页
    * @param pageNo 当前页
    * @param pageSize 每页条数
    * @param request request
     * @param userid 用户id
    * @return ResultVo
    */
    @ApiOperation(value = "获得分页数据（不同角色） ", notes = "获得分页数据（不同角色）  - 查询构造器")
//    @RequiresPermissions("ykfire_ykcheck_select")
    @Override
    public ResultVo<?> findPage(Integer pageNo, Integer pageSize, HttpServletRequest request,String userid,Integer status) {

        YkuserModel ykuserModel=iYkuserService.get(userid);
        QueryBuilder<Ykcheck> queryBuilder = new WebQueryBuilder<>(entityClazz, request.getParameterMap());
        QueryWrapper<Ykcheck> queryWrapper = queryBuilder.build();
        queryWrapper.eq("status",status);
        Page<Ykcheck, YkcheckModel> page = new Page<>(pageNo, pageSize);

        if(ykuserModel.getLimitChief()==1) {
            page.setQueryWrapper(queryWrapper);
            page = IService.findPage(page);
            return ResultVo.success(page.getPageData());
        }
        if(ykuserModel.getLimitAdviser()==1){
            queryWrapper.eq("adviser_id",ykuserModel.getId());
            page.setQueryWrapper(queryWrapper);
            page = IService.findPage(page);
            return ResultVo.success(page.getPageData());
        }
        if(ykuserModel.getLimitAduitor()==1){
            queryWrapper.eq("area_id",ykuserModel.getAreaId());
            page.setQueryWrapper(queryWrapper);
            page = IService.findPage(page);
            return ResultVo.success(page.getPageData());
        }
        if(ykuserModel.getLimitInspector()==1){
            queryWrapper.eq("person_id",ykuserModel.getId());
            page.setQueryWrapper(queryWrapper);
            page = IService.findPage(page);
            return ResultVo.success(page.getPageData());
        }
        return ResultVo.success();
    }

    /**
    * 检查信息表 保存
    * @param ykcheckModel 模型
    * @return ResultVo
    */
    @ApiOperation(value = "保存检查表", notes = "保存检查表")
//    @RequiresPermissions("ykfire_ykcheck_insertorupdate")
    @EnableLog
    @PostMapping("/saveOrUpdate")
    public ResultVo<?> saveOrUpdate(YkcheckModel ykcheckModel) throws InvocationTargetException, IllegalAccessException {


            String msg = "0";

            //如果不传id,则为保存
            if (ObjectUtil.isEmpty(ykcheckModel.getId())) {
                //状态为0 保存
                ykcheckModel.setStatus("0");

                //获取对应参谋长id
                YkcompanyModel ykcompanyModel = iYkcompanyService.get(ykcheckModel.getCompanyId());
                ykcheckModel.setAdviserId(ykcompanyModel.getAdviserId());
                //获取对应参谋长姓名
                YkuserModel ykuserModel = iYkuserService.get(ykcompanyModel.getAdviserId());
                ykcheckModel.setAdviserName(ykuserModel.getName());

                ykcheckModel = IService.insert(ykcheckModel);
                msg = "保存检查表成功";
            } else {//传id为修改
                YkcheckModel ykcheck = IService.get(ykcheckModel.getId());
                //先判断 数据库中是否以提交
                if(ykcheck.getStatus().equals("0")) {
//                    BeanUtils.copyProperties(ykcheck, ykcheckModel);

                    //获取对应参谋长id
                    YkcompanyModel ykcompanyModel = iYkcompanyService.get(ykcheckModel.getCompanyId());
                    ykcheckModel.setAdviserId(ykcompanyModel.getAdviserId());
                    //获取对应参谋长姓名
                    YkuserModel ykuserModel = iYkuserService.get(ykcompanyModel.getAdviserId());
                    ykcheckModel.setAdviserName(ykuserModel.getName());
                    ykcheckModel.setStatus("0");

                    IService.update(ykcheckModel);
                    msg = "修改检查表成功";
                }
                else return ResultVo.error("已提交表无法更改");
            }

            Map<String, Object> data = new HashedMap();
            data.put("id", ykcheckModel.getId());

            return ResultVo.success(msg, data);


    }

    /**
    * 检查信息表 修改
    * @param model 模型
    * @return ResultVo
    */
    @ApiOperation(value = "修改检查信息表数据", notes = "修改检查信息表数据")
//    @RequiresPermissions("ykfire_ykcheck_update")
    @EnableLog
    @Override
    public ResultVo<?> update(YkcheckModel model) {
        // 调用修改方法
        IService.update(model);
        return ResultVo.success("修改检查信息表成功");
    }


    /**
    * 检查信息表 删除
    * @param id ID
    * @return ResultVo
    */
    @ApiOperation(value = "删除检查信息表数据", notes = "删除检查信息表数据")
//    @RequiresPermissions("ykfire_ykcheck_update")
    @EnableLog
    @Override
    public ResultVo<?> del(String id){
        IService.delete(id);
        return ResultVo.success("删除检查信息表成功");
    }

    /**
    * 检查信息表 批量删除
    * @param ids ID 数组
    * @return ResultVo
    */
    @ApiOperation(value = "批量删除检查信息表数据", notes = "批量删除检查信息表数据")
//    @RequiresPermissions("ykfire_ykcheck_update")
    @EnableLog
    @Override
    public ResultVo<?> delAll(String ids){
        String[] idArray = Convert.toStrArray(ids);
        IService.deleteAll(idArray);
        return ResultVo.success("批量删除检查信息表成功");
    }


    /**
    * 检查信息表 Excel 导出
    * 注：这里 RequiresPermissionsCus 引入的是 自定义鉴权注解
    *
    * 导出时，Token认证和方法权限认证 全部都由自定义完成
    * 因为在 导出不成功时，需要推送错误信息，
    * 前端直接走下载流，当失败时无法获得失败信息，即使前后端换一种方式后端推送二进制文件前端再次解析也是最少2倍的耗时
    * ，且如果数据量过大，前端进行渲染时直接会把浏览器卡死
    * 而直接开启socket接口推送显然是太过浪费资源了，所以目前采用Java最原始的手段
    * response 推送 javascript代码 alert 提示报错信息
    *
    * @param request request
    * @param response response
    */
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
//    @RequiresPermissionsCus("ykfire_ykcheck_export")
    @EnableLog
    @Override
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
        // 当前方法
        Method method = ReflectUtil.getMethodByName(this.getClass(), "exportExcel");
        QueryBuilder<Ykcheck> queryBuilder = new WebQueryBuilder<>(entityClazz, request.getParameterMap());
        super.excelExport(YkcheckRestApi.SUB_TITLE, queryBuilder.build(), response, method);
    }

    /**
    * 检查信息表 Excel 导入
    * 注：这里 RequiresPermissions 引入的是 Shiro原生鉴权注解
    * @param request 文件流 request
    * @return ResultVo
    */
    @ApiOperation(value = "导入Excel", notes = "导入Excel")
//    @RequiresPermissions("ykfire_ykcheck_import")
    @EnableLog
    @Override
    public ResultVo<?> importExcel(MultipartHttpServletRequest request) {
        return super.importExcel(request);
    }

    /**
    * 检查信息表 Excel 下载导入模版
    * 注：这里 RequiresPermissionsCus 引入的是 自定义鉴权注解
    * @param response response
    */
    @ApiOperation(value = "导出Excel模版", notes = "导出Excel模版")
//    @RequiresPermissionsCus("ykfire_ykcheck_import")
    @Override
    public void importTemplate(HttpServletResponse response) {
        // 当前方法
        Method method = ReflectUtil.getMethodByName(this.getClass(), "importTemplate");
        super.importTemplate(YkcheckRestApi.SUB_TITLE, response, method);
    }


    /**
     * 柱状图（表，区域）统计近六个月数据
     * @return ResultVo
     */
    @ApiOperation(value = "柱状图（表，区域）统计近六个月数据", notes = "柱状图（表，区域）统计近六个月数据")
//    @RequiresPermissions("ykfire_ykcheck_lastmonth")
    @GetMapping("/histogram")
    public ResultVo<?> histogram( ) throws ParseException {

        YkcheckRestController ykcheckRestController =new YkcheckRestController();
//        List<Map<String, Object>> list1 = new ArrayList<>();
        Map map1 = new LinkedHashMap<String, Object>();

        //获取最近六个月 月份
        Calendar cal = Calendar.getInstance();
//        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int months[] = new int[6];
        for (int k = 0; k <= 5; k++) {
            if (month - 5 + k <= 0) {
                month += 12;
            }
            if (month - 5 + k > 12) {
                month = month - 12;
            }
            months[k] = month - 5 + k;
        }
        map1.put("categories",months);



        //遍历 区域数据库 读取区域id
        QueryBuilder<Ykarea> queryBuilder = new GenQueryBuilder<>();
        QueryWrapper<Ykarea> queryWrapper = queryBuilder.build();
        queryWrapper.isNotNull("id");
        List<Ykarea> list3 =iYkareaService.findList(queryWrapper);



        List<Object> series=new LinkedList<>();
        //外层区域 内存时间
        for (int k=0;k<=2;k++) {
            for (int i = 0; i <= list3.size() - 1; i++) {

                Map itemMap = new LinkedHashMap<String, Object>();
                itemMap.put("name", "表"+k+ list3.get(i).getName() + "不合格数量");
                Integer data[] = new Integer[6];
                for (int j = 0; j <= 5; j++) {
                    data[j] = IService.counttable(5 - j, 1, list3.get(i).getId(),k);
                }
                itemMap.put("data", data);
                series.add(itemMap);

            }

            for (int i = 0; i <= list3.size() - 1; i++) {

                Map itemMap = new LinkedHashMap<String, Object>();
                itemMap.put("name", "表"+k+list3.get(i).getName() + "合格数量");
                Integer data[] = new Integer[6];
                for (int j = 0; j <= 5; j++) {
                    data[j] = IService.counttable(5 - j, 0, list3.get(i).getId(),k);
                }
                itemMap.put("data", data);
                series.add(itemMap);

            }
        }
        map1.put("series",series);
        return ResultVo.success(map1);
    }








//
////        YkcheckRestController ykcheckRestController =new YkcheckRestController();
//        List<Map<String, Object>> list1 = new ArrayList<>();
//
//        //获取最近六个月 月份
//        Calendar cal = Calendar.getInstance();
////        int year = cal.get(Calendar.YEAR);
//        int month = cal.get(Calendar.MONTH) + 1;
//
//        System.out.println("111111111");
//
//
//
//        //遍历 区域数据库 读取区域id
//        QueryBuilder<Ykarea> queryBuilder = new GenQueryBuilder<>();
//        QueryWrapper<Ykarea> queryWrapper = queryBuilder.build();
//        queryWrapper.isNotNull("id");
//        List<Ykarea> list3 =iYkareaService.findList(queryWrapper);
//
//
//
//        //外层区域 内存时间
//        for(int i=0;i<=list3.size()-1;i++){
//
//            int correct0[]=new int[6];
//            int correct1[]=new int[6];
//
//
//            for(int j=0;j<=5;j++){
//
//
//
//                correct0[j]=IService.countmonth(5-j,0,list3.get(i).getId());
//
//                correct1[j]=IService.countmonth(5-j,1,list3.get(i).getId());
//
//                if(j==5){
//
//                    System.out.println("区域id=========="+list3.get(i).getId());
//
////                    for (int a=0;a<=correct0.length-1;a++){
////                        System.out.println("correct0==="+correct0[a]);
////                    }
////                    for (int a=0;a<=correct1.length-1;a++){
////                        System.out.println("correct1==="+correct1[a]);
////                    }
//
//
//                    int months[] = new int[6];
//                    for (int k = 0; k <= 5; k++) {
//                        if (month - 5 + k <= 0) {
//                            month += 12;
//                        }
//                        if (month - 5 + k > 12) {
//                            month = month - 12;
//                        }
//                        months[k] = month - 5 + k;
//                    }
//
//
//
//                    List<Map<String, Object>> list2 = new ArrayList<>();
//
//                    Map map1 = new LinkedHashMap<String, Object>();
//                    Map map2 = new LinkedHashMap<String, Object>();
//                    Map map3 = new LinkedHashMap<String, Object>();
//
//                    System.out.println("2222222222222222");
//
//                    map1.put("最近6个月", months);
//                    map2.put("name",list3.get(i).getName()+"合格数量");
//                    map2.put("data",correct0);
//                    map3.put("name",list3.get(i).getName()+"不合格数量");
//                    map3.put("data",correct1);
//
//
//                    list2.add(map2);
//                    list2.add(map3);
//                    map1.put(list3.get(i).getName()+"区域",list2);
//
//
//                    list1.add(map1);
//                }
//
//            }
//
//        }
//
//        return ResultVo.success(list1);
//    }


    /**
     * 折线图（区域，不合格）统近六个月数据
     * @return ResultVo
     */
    @ApiOperation(value = "折线图 统计近六个月数据", notes = "折线图 统计近六个月数据")
//    @RequiresPermissions("ykfire_ykcheck_lastmonth")
    @GetMapping("/line")
    public ResultVo<?> line( ) throws ParseException {

//        YkcheckRestController ykcheckRestController =new YkcheckRestController();
//        List<Map<String, Object>> list1 = new ArrayList<>();
        Map map1 = new LinkedHashMap<String, Object>();

        //获取最近六个月 月份
        Calendar cal = Calendar.getInstance();
//        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int months[] = new int[6];
                    for (int k = 0; k <= 5; k++) {
                        if (month - 5 + k <= 0) {
                            month += 12;
                        }
                        if (month - 5 + k > 12) {
                            month = month - 12;
                        }
                        months[k] = month - 5 + k;
                    }
                    map1.put("categories",months);
        System.out.println("111111111");



        //遍历 区域数据库 读取区域id
        QueryBuilder<Ykarea> queryBuilder = new GenQueryBuilder<>();
        QueryWrapper<Ykarea> queryWrapper = queryBuilder.build();
        queryWrapper.isNotNull("id");
        List<Ykarea> list3 =iYkareaService.findList(queryWrapper);



        List<Object> series=new LinkedList<>();
        //外层区域 内存时间
        for(int i=0;i<=list3.size()-1;i++){

            Map itemMap = new LinkedHashMap<String, Object>();
            itemMap.put("name",list3.get(i).getName());
            Integer data[]=new Integer[6];
            for(int j=0;j<=5;j++){
                data[j]=IService.countmonth(5-j,1,list3.get(i).getId());
            }
            itemMap.put("data",data);
            series.add(itemMap);

        }
        map1.put("series",series);
        return ResultVo.success(map1);
    }

    /**
     * 饼图（行业）企业数量
     * @return ResultVo
     */
    @ApiOperation(value = "饼图（行业）企业数量", notes = "饼图（行业）企业数量")
//    @RequiresPermissions("ykfire_ykcheck_lastmonth")
    @GetMapping("/pieone")
    public ResultVo<?> pieone( ) {

        String professions[] = {"农、林、牧、渔业", "采矿业", "制造业", "电力、热力、燃气及水生产和供应业", "建筑业"
                , "交通运输、仓储和邮政业", "信息传输、计算机服务和软件业", "批发和零售业", "住宿和餐饮业", "金融业"
                , "房地产业", "租赁和商务服务业", "科学研究和技术服务业", "水利、环境和公共设施管理业", "居民服务、修理和其他服务业"
                , "教育", "卫生和社会工作", "文化、体育和娱乐业", "公共管理、社会保障和社会组织", "国际组织"};

        Map map1 = new LinkedHashMap<String, Object>();
        Map data = new LinkedHashMap<String, Object>();
        List<Object> series = new LinkedList<>();
        List<Object> list = new LinkedList<>();



        for (int i = 0; i < professions.length; i++) {

            Map itemMap = new LinkedHashMap<String, Object>();
            QueryBuilder<Ykcompany> queryBuilder = new GenQueryBuilder<>();
            QueryWrapper<Ykcompany> queryWrapper = queryBuilder.build();
            queryWrapper.eq("profession", professions[i]);
            Integer a = iYkcompanyService.count(queryWrapper);
            System.out.println("行业=============="+professions[i]);

            itemMap.put("name", professions[i]);
            itemMap.put("value", a);
            list.add(itemMap);
            System.out.println("个数================================="+a);

        }
        data.put("data",list);
        series.add(data);
        map1.put("series",series);
            return ResultVo.success(map1);
        }


    /**
     * 饼图（行业）整改数量
     * @return ResultVo
     */
    @ApiOperation(value = "饼图（行业）整改数量", notes = "饼图（行业）整改数量")
//    @RequiresPermissions("ykfire_ykcheck_lastmonth")
    @GetMapping("/pietwo")
    public ResultVo<?> pietwo( ) {

        String professions[] = {"农、林、牧、渔业", "采矿业", "制造业", "电力、热力、燃气及水生产和供应业", "建筑业"
                , "交通运输、仓储和邮政业", "信息传输、计算机服务和软件业", "批发和零售业", "住宿和餐饮业", "金融业"
                , "房地产业", "租赁和商务服务业", "科学研究和技术服务业", "水利、环境和公共设施管理业", "居民服务、修理和其他服务业"
                , "教育", "卫生和社会工作", "文化、体育和娱乐业", "公共管理、社会保障和社会组织", "国际组织"};

        Map map1 = new LinkedHashMap<String, Object>();
        Map data = new LinkedHashMap<String, Object>();
        List<Object> series = new LinkedList<>();
        List<Object> list = new LinkedList<>();



        //循环每个行业
        for (int i = 0; i < professions.length; i++) {

            Map itemMap = new LinkedHashMap<String, Object>();
            QueryBuilder<Ykcompany> queryBuilder = new GenQueryBuilder<>();
            QueryWrapper<Ykcompany> queryWrapper = queryBuilder.build();
            queryWrapper.eq("profession", professions[i]);
            //获取对应行业的 所有公司
            List<Ykcompany>listcompany=iYkcompanyService.findList(queryWrapper);
            Integer b =0;

            //循环行业对应每个公司 根据公司Id 去check表 查询整改数量
            //每条check都循环
            for (int j=0;j<listcompany.size();j++){
                QueryBuilder<Ykcheck>queryBuilder1=new GenQueryBuilder<>();
                QueryWrapper<Ykcheck>queryWrapper1=queryBuilder1.build();
                queryWrapper1.eq("company_id",listcompany.get(j).getId())
                             .eq("correct",1);
                b+=IService.count(queryWrapper1);
            }

            itemMap.put("name", professions[i]);
            itemMap.put("value", b);
            list.add(itemMap);
            System.out.println("行业=============="+professions[i]);
            System.out.println("个数================================="+b);
        }
        data.put("data",list);
        series.add(data);
        map1.put("series",series);
        return ResultVo.success(map1);
    }




    /**
     * 累计走访企业次数
     * @return ResultVo
     */
    @ApiOperation(value = "累计走访企业次数", notes = "累计走访企业次数")
//    @RequiresPermissions("ykfire_ykcheck_lastmonth")
    @GetMapping("/checkcount")
    public ResultVo<?> checkcount( ) {
        QueryBuilder<Ykcheck> queryBuilder = new GenQueryBuilder<>();
        QueryWrapper<Ykcheck> queryWrapper = queryBuilder.build();
        queryWrapper.isNotNull("company_id");

        return ResultVo.success(IService.count(queryWrapper));
    }


    /**
     * 解决隐患次数
     * @return ResultVo
     */
    @ApiOperation(value = "解决隐患次数", notes = "解决隐患次数")
//    @RequiresPermissions("ykfire_ykcheck_lastmonth")
    @GetMapping("/solvecount")
    public ResultVo<?> solvecount( ) {
        QueryBuilder<Ykcheck> queryBuilder = new GenQueryBuilder<>();
        QueryWrapper<Ykcheck> queryWrapper = queryBuilder.build();
        queryWrapper.eq("correct",1)
                    .eq("status",3);
        return ResultVo.success(IService.count(queryWrapper));
    }

    /**
     * 服务群众人次
     * @return ResultVo
     */
    @ApiOperation(value = "服务群众人次", notes = "服务群众人次")
//    @RequiresPermissions("ykfire_ykcheck_lastmonth")
    @GetMapping("/peoplecount")
    public ResultVo<?> peoplecount( ) {
        QueryBuilder<Ykcompany> queryBuilder = new GenQueryBuilder<>();
        QueryWrapper<Ykcompany> queryWrapper = queryBuilder.build();
        queryWrapper.isNotNull("id");
        List<Ykcompany>list=iYkcompanyService.findList(queryWrapper);
        long a =0;

        for(int i=0;i<list.size();i++){
            QueryBuilder<Ykcheck> queryBuilder1 = new GenQueryBuilder<>();
            QueryWrapper<Ykcheck> queryWrapper1 = queryBuilder1.build();
            queryWrapper1
                    .eq("company_id",list.get(i).getId())
                    .eq("status",3);
            a=a+IService.count(queryWrapper1)*list.get(i).getPeople();
        }

        return ResultVo.success(a);
    }
//public static void main (String[] args) throws ParseException {
//       YkcheckRestController ykcheckRestController =new YkcheckRestController();
//       ykcheckRestController.lastmonth();
//
//}


}